package com.jtjrenren.android.taxi.passenger.core.pay.charge;

import android.app.Activity;
import android.content.DialogInterface;
import android.content.Intent;
import android.widget.CheckBox;
import android.widget.TextView;

import com.jtjrenren.android.taxi.passenger.R;
import com.jtjrenren.android.taxi.passenger.base.BaseActivity;
import com.jtjrenren.android.taxi.passenger.bean.api.ApiCommonT;
import com.jtjrenren.android.taxi.passenger.bean.api.ApiPayMsg;
import com.jtjrenren.android.taxi.passenger.bean.event.AliPayEvent;
import com.jtjrenren.android.taxi.passenger.manger.AppManger;
import com.jtjrenren.android.taxi.passenger.manger.api.ApiWorks;
import com.jtjrenren.android.taxi.passenger.manger.pay.ali.AliPayManger;
import com.jtjrenren.android.taxi.passenger.manger.pay.weixin.WxPayManger;
import com.jtjrenren.android.taxi.passenger.utils.ModuleUtils;
import com.jtjrenren.android.taxi.passenger.utils.UIHelper;
import com.library.utils.StringUtils;
import com.tencent.mm.sdk.modelbase.BaseResp;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import butterknife.BindView;
import butterknife.OnCheckedChanged;
import butterknife.OnClick;

/**
 * Created by wdl on 2017/1/6.
 */

public class ChargeActivity extends BaseActivity {
    @BindView(R.id.title)
    TextView tv_title;
    @BindView(R.id.pay_charge_amount)
    TextView tv_rcharge_count;
    @BindView(R.id.pay_main_alizhifu_cb)
    CheckBox cb_ali;
    @BindView(R.id.pay_main_weixinzhifu_cb)
    CheckBox cb_weixin;

    private String payMode = "1"; //"0": 打车钱包  "1":支付宝；"2": 微信；3" :信用卡 "4" :储蓄卡 "5" :其他 "6":现金


    public static Intent getDiyIntent(Activity context, String money) {
        Intent intent = new Intent(context, ChargeActivity.class);
        intent.putExtra("money", money);
        return intent;
    }

    @Override
    public int getLayoutId() {
        return R.layout.aty_pay_charge;
    }

    @Override
    public void init() {
        EventBus.getDefault().register(this);
        tv_title.setText("充值");

        String money = getIntent().getStringExtra("money");
        tv_rcharge_count.setText(money);

        cb_ali.setChecked(true);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();

        EventBus.getDefault().unregister(this);
    }

    @OnClick(R.id.title_left)
    public void clickBack() {
        finish();
    }

    @OnCheckedChanged(R.id.pay_main_alizhifu_cb)
    void onCheckedAL(boolean checked) {
        if (checked) {
            payMode = "1";
        }
    }

    @OnCheckedChanged(R.id.pay_main_weixinzhifu_cb)
    void onCheckedWx(boolean checked) {
        if (checked) {
            payMode = "2";
        }
    }

    @OnClick(R.id.pay_main_alizhifu)
    public void clickALI() {
        refreshPayTypeView();
        cb_ali.setChecked(true);
    }

    @OnClick(R.id.pay_main_weixinzhifu)
    public void clickWX() {
        refreshPayTypeView();
        cb_weixin.setChecked(true);
    }

    @OnClick(R.id.pay_main_commit)
    public void clickCommit() {
        UIHelper.getAlertDialog(this, this.getString(R.string.querenzhifu), new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
                charge();
            }
        }, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
                charge();
            }
        }).show();
    }

    /**
     * 进行充值
     */
    private void charge() {
        showWaittingDialog("交易进行中，请稍候...", false);
        String money = tv_rcharge_count.getText().toString();
        money = ModuleUtils.DesEncry(money);

        ApiWorks.payN(AppManger.getInstance().user.getWorkNo(), "1", payMode, money,
                "", "", "0", new ApiWorks.ResponseListener<String>() {
                    @Override
                    public void onResponse(String result) {
                        dismissWaittingDialog();
                        ApiCommonT commonApi = ModuleUtils.getCommonApi(result, ApiPayMsg.class);
                        if (commonApi.getCode() == 1 && commonApi.getData() != null && commonApi.getData().size() > 0) {
                            String data = ((ApiPayMsg) commonApi.getData().get(0)).getPostData();
                            if (payMode.equals("2")) {
                                WxPayManger wxPayManger = new WxPayManger();
                                wxPayManger.initManger(ChargeActivity.this);
                                if (!wxPayManger.pay2(data)) {
                                    displayShort("当前环境不支持微信支付");
                                }
                            } else if (payMode.equals("1")) {
                                AliPayManger manger = new AliPayManger();
                                manger.pay2(ChargeActivity.this, data);
                            }
                        } else {
                            displayShort("支付异常，请稍候重试...");
                        }
                    }
                });

    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void handAliPayEvent(AliPayEvent event) {
        String ali = ModuleUtils.handAliPayResult(event);
        boolean result = false;
        if (StringUtils.isEmpty(ali)) {
            result = true;
        }
        payResult(result, ali);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void handWxPayEvent(BaseResp event) {
        String we = ModuleUtils.handWeixinPayResult(event);
        boolean result = false;
        if (StringUtils.isEmpty(we)) {
            result = true;
        }
        payResult(result, we);
    }

    private void payResult(boolean success, String desc) {
        if (success) {
            finish();
        } else {
            displayShort(desc);
        }
    }

    public void refreshPayTypeView() {
        cb_ali.setChecked(false);
        cb_weixin.setChecked(false);
    }
}
